package Demo;

import java.util.Scanner;

public class TwinPrimes {

    // 判断是否为素数的方法
    public static boolean isPrime(int number) {
        if (number <= 1) {
            return false;
        }
        for (int i = 2; i <= Math.sqrt(number); i++) {
            if (number % i == 0) {
                return false;
            }
        }
        return true;
    }

    // 查找孪生素数对的方法
    public static void findTwinPrimes(int lowerLimit, int upperLimit) {
        int previousPrime = -1;

        for (int i = lowerLimit; i <= upperLimit - 2; i++) {
            if (isPrime(i) && isPrime(i + 2)) {
                if (i != previousPrime) {
                    System.out.println("(" + i + ", " + (i + 2) + ")");
                    previousPrime = i + 2;
                }
            }
        }
    }

    // 主方法
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // 用户输入范围
        System.out.print("请输入下限: ");
        int lowerLimit = scanner.nextInt();
        System.out.print("请输入上限: ");
        int upperLimit = scanner.nextInt();

        // 查找并输出孪生素数对
        findTwinPrimes(lowerLimit, upperLimit);
    }
}
